{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Creating Satellite Image Time Series and Timelapse Animations\n",
    "\n",
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/workshops/GEE_Workshop_2022_Part1.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## The map function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "myList = ee.List.sequence(1, 10)\n",
    "print(myList.getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def computeSquares(number):\n",
    "    return ee.Number(number).pow(2)\n",
    "\n",
    "\n",
    "squares = myList.map(computeSquares)\n",
    "print(squares.getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = myList.map(lambda number: ee.Number(number).pow(2))\n",
    "print(squares.getInfo())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Creating cloud-free composites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "fc = ee.FeatureCollection(\"USDOS/LSIB_SIMPLE/2017\").filter(\n",
    "    ee.Filter.eq(\"country_na\", \"Netherlands\")\n",
    ")\n",
    "\n",
    "Map.addLayer(fc, {\"color\": \"ff000000\"}, \"Netherlands\")\n",
    "Map.centerObject(fc)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "years = ee.List.sequence(2013, 2021)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "def yearly_image(year):\n",
    "    start_date = ee.Date.fromYMD(year, 1, 1)\n",
    "    end_date = start_date.advance(1, \"year\")\n",
    "\n",
    "    collection = (\n",
    "        ee.ImageCollection(\"LANDSAT/LC08/C01/T1\")\n",
    "        .filterDate(start_date, end_date)\n",
    "        .filterBounds(fc)\n",
    "    )\n",
    "\n",
    "    image = ee.Algorithms.Landsat.simpleComposite(collection).clipToCollection(fc)\n",
    "\n",
    "    return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = years.map(yearly_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_params = {\"bands\": [\"B5\", \"B4\", \"B3\"], \"max\": 128}\n",
    "for index in range(0, 9):\n",
    "    image = ee.Image(images.get(index))\n",
    "    layer_name = \"Year \" + str(index + 2013)\n",
    "    Map.addLayer(image, vis_params, layer_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "## Creating timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = ee.ImageCollection(\"COPERNICUS/S2_HARMONIZED\").filterMetadata(\n",
    "    \"CLOUDY_PIXEL_PERCENTAGE\", \"less_than\", 10\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2016-01-01\"\n",
    "end_date = \"2022-12-31\"\n",
    "region = ee.Geometry.BBox(-122.5549, 37.6968, -122.3446, 37.8111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = geemap.create_timeseries(\n",
    "    collection, start_date, end_date, region, frequency=\"year\", reducer=\"median\"\n",
    ")\n",
    "images.size().getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "vis_params = {\"min\": 0, \"max\": 4000, \"bands\": [\"B8\", \"B4\", \"B3\"]}\n",
    "labels = [str(y) for y in range(2016, 2023)]\n",
    "\n",
    "Map.addLayer(images, vis_params, \"Sentinel-2\", False)\n",
    "Map.add_time_slider(images, vis_params, time_interval=2, labels=labels)\n",
    "Map.centerObject(region)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "## NAIP timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-99.1019, 47.1274, -99.0334, 47.1562)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = geemap.naip_timeseries(roi, start_year=2009, end_year=2022, RGBN=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "years = geemap.image_dates(collection, date_format=\"YYYY\").getInfo()\n",
    "print(years)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "size = len(years)\n",
    "images = collection.toList(size)\n",
    "for i in range(size):\n",
    "    image = ee.Image(images.get(i))\n",
    "    Map.addLayer(image, {\"bands\": [\"N\", \"R\", \"G\"]}, years[i])\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.naip_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"naip.gif\",\n",
    "    bands=[\"N\", \"R\", \"G\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"NAIP Timelapse\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/Me6D78k.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "## Landsat timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-74.7222, -8.5867, -74.1596, -8.2824)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"landsat.gif\",\n",
    "    start_year=1984,\n",
    "    end_year=2022,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Landsat Timelapse\",\n",
    "    progress_bar_color=\"blue\",\n",
    "    mp4=True,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/VL1V1Y4.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n",
    "Map.addLayer(roi)\n",
    "Map.centerObject(roi)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"las_vegas.gif\",\n",
    "    start_year=1984,\n",
    "    end_year=2022,\n",
    "    bands=[\"NIR\", \"Red\", \"Green\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Las Vegas, NV\",\n",
    "    font_color=\"blue\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/LzWyyZW.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n",
    "Map.addLayer(roi)\n",
    "Map.centerObject(roi)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"hong_kong.gif\",\n",
    "    start_year=1990,\n",
    "    end_year=2022,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"Hong Kong\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/0nLRj22.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39",
   "metadata": {},
   "source": [
    "## Sentinel-1 timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(117.1132, 3.5227, 117.2214, 3.5843)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.sentinel1_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"sentinel1.gif\",\n",
    "    start_year=2019,\n",
    "    end_year=2019,\n",
    "    start_date=\"04-01\",\n",
    "    end_date=\"08-01\",\n",
    "    frequency=\"day\",\n",
    "    vis_params={\"min\": -30, \"max\": 0},\n",
    "    palette=\"Greys\",\n",
    "    frames_per_second=3,\n",
    "    title=\"Sentinel-1 Timelapse\",\n",
    "    add_colorbar=True,\n",
    "    colorbar_bg_color=\"gray\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43",
   "metadata": {},
   "source": [
    "## Sentinel-2 timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-74.7222, -8.5867, -74.1596, -8.2824)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.sentinel2_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"sentinel2.gif\",\n",
    "    start_year=2016,\n",
    "    end_year=2021,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    frequency=\"year\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"Sentinel-2 Timelapse\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/BmQdo9j.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48",
   "metadata": {},
   "source": [
    "## MODIS timelapse\n",
    "\n",
    "### MODIS NDVI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.modis_ndvi_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"ndvi.gif\",\n",
    "    data=\"Terra\",\n",
    "    band=\"NDVI\",\n",
    "    start_date=\"2000-01-01\",\n",
    "    end_date=\"2021-12-31\",\n",
    "    frames_per_second=3,\n",
    "    title=\"MODIS NDVI Timelapse\",\n",
    "    overlay_data=\"countries\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/KZwf8c2.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53",
   "metadata": {},
   "source": [
    "### MODIS temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-171.21, -57.13, 177.53, 79.99)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.modis_ocean_color_timelapse(\n",
    "    satellite=\"Aqua\",\n",
    "    start_date=\"2018-01-01\",\n",
    "    end_date=\"2020-12-31\",\n",
    "    roi=roi,\n",
    "    frequency=\"month\",\n",
    "    out_gif=\"temperature.gif\",\n",
    "    overlay_data=\"continents\",\n",
    "    overlay_color=\"yellow\",\n",
    "    overlay_opacity=0.5,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/ELVn5jq.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58",
   "metadata": {},
   "source": [
    "## GOES timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2022-01-15T03:00:00\"\n",
    "end_date = \"2022-01-15T07:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_timelapse(\n",
    "    roi, \"goes.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/l67i6Pj.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088)\n",
    "start_date = \"2021-10-24T14:00:00\"\n",
    "end_date = \"2021-10-25T01:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_timelapse(\n",
    "    roi, \"hurricane.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/zYt2b8d.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490)\n",
    "Map.addLayer(roi)\n",
    "Map.centerObject(roi)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2020-09-05T15:00:00\"\n",
    "end_date = \"2020-09-06T02:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_fire_timelapse(\n",
    "    roi, \"fire.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/pgPjuLS.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71",
   "metadata": {},
   "source": [
    "## Fading effects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_gif = \"https://i.imgur.com/ZWSZC5z.gif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(in_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = \"gif_fading.gif\"\n",
    "geemap.gif_fading(in_gif, out_gif, verbose=False)\n",
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/PbK89b6.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.BBox(-69.3154, -22.8371, -69.1900, -22.7614)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"mines.gif\",\n",
    "    start_year=2004,\n",
    "    end_year=2010,\n",
    "    frames_per_second=1,\n",
    "    title=\"Copper mines, Chile\",\n",
    "    fading=True,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/1ZbWs66.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79",
   "metadata": {},
   "source": [
    "## Adding animated text\n",
    "\n",
    "### Add animated text to an existing GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/gee-community/geemap/raw/master/examples/data/animation.gif\"\n",
    "filename = \"animation.gif\"\n",
    "geemap.download_file(url, filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82",
   "metadata": {},
   "source": [
    "#### Add animated text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = \"timelapse.gif\"\n",
    "geemap.add_text_to_gif(\n",
    "    filename,\n",
    "    out_gif,\n",
    "    xy=(\"5%\", \"5%\"),\n",
    "    text_sequence=1984,\n",
    "    font_size=30,\n",
    "    font_color=\"#0000ff\",\n",
    "    duration=100,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85",
   "metadata": {},
   "source": [
    "#### Add place name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    out_gif, out_gif, xy=(\"35%\", \"85%\"), text_sequence=\"Las Vegas\", font_color=\"black\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88",
   "metadata": {},
   "source": [
    "### Create GIF from Earth Engine data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89",
   "metadata": {},
   "source": [
    "#### Prepare for an ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "\n",
    "geemap.ee_initialize()\n",
    "\n",
    "# Define an area of interest geometry with a global non-polar extent.\n",
    "aoi = ee.Geometry.Polygon(\n",
    "    [[[-179.0, 78.0], [-179.0, -58.0], [179.0, -58.0], [179.0, 78.0]]], None, False\n",
    ")\n",
    "\n",
    "# Import hourly predicted temperature image collection for northern winter\n",
    "# solstice. Note that predictions extend for 384 hours; limit the collection\n",
    "# to the first 24 hours.\n",
    "tempCol = (\n",
    "    ee.ImageCollection(\"NOAA/GFS0P25\")\n",
    "    .filterDate(\"2018-12-22\", \"2018-12-23\")\n",
    "    .limit(24)\n",
    "    .select(\"temperature_2m_above_ground\")\n",
    ")\n",
    "\n",
    "# Define arguments for animation function parameters.\n",
    "videoArgs = {\n",
    "    \"dimensions\": 768,\n",
    "    \"region\": aoi,\n",
    "    \"framesPerSecond\": 10,\n",
    "    \"crs\": \"EPSG:3857\",\n",
    "    \"min\": -40.0,\n",
    "    \"max\": 35.0,\n",
    "    \"palette\": [\"blue\", \"purple\", \"cyan\", \"green\", \"yellow\", \"red\"],\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91",
   "metadata": {},
   "source": [
    "#### Save the GIF to local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92",
   "metadata": {},
   "outputs": [],
   "source": [
    "saved_gif = \"temperature.gif\"\n",
    "geemap.download_ee_video(tempCol, videoArgs, saved_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(saved_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94",
   "metadata": {},
   "source": [
    "#### Generate an hourly text sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96",
   "metadata": {},
   "source": [
    "#### Add text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = \"output2.gif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    saved_gif,\n",
    "    out_gif,\n",
    "    xy=(\"3%\", \"5%\"),\n",
    "    text_sequence=text,\n",
    "    font_size=30,\n",
    "    font_color=\"#ffffff\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    out_gif,\n",
    "    out_gif,\n",
    "    xy=(\"32%\", \"92%\"),\n",
    "    text_sequence=\"NOAA GFS Hourly Temperature\",\n",
    "    font_color=\"white\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "100",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "101",
   "metadata": {},
   "source": [
    "### Adding colorbar to an image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "102",
   "metadata": {},
   "source": [
    "#### Download a GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "103",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://i.imgur.com/MSde1om.gif\"\n",
    "in_gif = \"temp.gif\"\n",
    "geemap.download_file(url, \"temp.gif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "104",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(in_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105",
   "metadata": {},
   "source": [
    "#### Get image URLs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "106",
   "metadata": {},
   "outputs": [],
   "source": [
    "noaa_logo = \"https://bit.ly/3ahJoMq\"\n",
    "ee_logo = \"https://i.imgur.com/Qbvacvm.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107",
   "metadata": {},
   "source": [
    "#### Set output GIF path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "108",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = \"output.gif\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "109",
   "metadata": {},
   "source": [
    "#### Add images to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "110",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_image_to_gif(\n",
    "    in_gif, out_gif, in_image=noaa_logo, xy=(\"2%\", \"80%\"), image_size=(80, 80)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "111",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_image_to_gif(\n",
    "    out_gif, out_gif, in_image=ee_logo, xy=(\"13%\", \"79%\"), image_size=(85, 85)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "112",
   "metadata": {},
   "source": [
    "#### Display output GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "113",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "114",
   "metadata": {},
   "source": [
    "#### Create a colorbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "115",
   "metadata": {},
   "outputs": [],
   "source": [
    "width = 250\n",
    "height = 30\n",
    "palette = [\"blue\", \"purple\", \"cyan\", \"green\", \"yellow\", \"red\"]\n",
    "labels = [-40, 35]\n",
    "colorbar = geemap.create_colorbar(\n",
    "    width=width,\n",
    "    height=height,\n",
    "    palette=palette,\n",
    "    vertical=False,\n",
    "    add_labels=True,\n",
    "    font_size=20,\n",
    "    labels=labels,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "116",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(colorbar)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "117",
   "metadata": {},
   "source": [
    "#### Add colorbar to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "118",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_image_to_gif(\n",
    "    out_gif, out_gif, in_image=colorbar, xy=(\"69%\", \"89%\"), image_size=(250, 250)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "119",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
